% routine save_results
% save simulated results in a user-defined file

HandleEditTextHR=findobj(gcf,'Tag','EditTextHR');
HR=str2num(get(HandleEditTextHR,'String'));

HandleEditTextEmaxV=findobj(gcf,'Tag','EditTextEmaxV');
EmaxV=str2num(get(HandleEditTextEmaxV,'String'));

HandleEditTextTEmaxV=findobj(gcf,'Tag','EditTextTEmaxV');
TEmaxV=str2num(get(HandleEditTextTEmaxV,'String'));

HandleEditTextEminV=findobj(gcf,'Tag','EditTextEminV');
EminV=str2num(get(HandleEditTextEminV,'String'));

HandleEditTextVdeadV=findobj(gcf,'Tag','EditTextVdeadV');
VdeadV=str2num(get(HandleEditTextVdeadV,'String'));

HandleEditTextPvenous=findobj(gcf,'Tag','EditTextPvenous');
Pvenous=str2num(get(HandleEditTextPvenous,'String'));

HandleEditTextZ0=findobj(gcf,'Tag','EditTextZ0');
Z0=str2num(get(HandleEditTextZ0,'String'));

HandleEditTextL=findobj(gcf,'Tag','EditTextL');
L=str2num(get(HandleEditTextL,'String'));

HandleEditTextC=findobj(gcf,'Tag','EditTextC');
C=str2num(get(HandleEditTextC,'String'));

HandleEditTextR=findobj(gcf,'Tag','EditTextR');
R=str2num(get(HandleEditTextR,'String'));

HandleEditTextT_interval=findobj(gcf,'Tag','EditTextT_interval');
t_interval=str2num(get(HandleEditTextT_interval,'String'));

HandleEditTextRmv=findobj(gcf,'Tag','EditTextRmv');
Rmvfwd=str2num(get(HandleEditTextRmv,'String'));

HandleEditTextRao=findobj(gcf,'Tag','EditTextRao');
Raofwd=str2num(get(HandleEditTextRao,'String'));

HandleEditTextRmvbwd=findobj(gcf,'Tag','EditTextRmvbwd');
Rmvbwd=str2num(get(HandleEditTextRmvbwd,'String'));

HandleEditTextRaobwd=findobj(gcf,'Tag','EditTextRaobwd');
Raobwd=str2num(get(HandleEditTextRaobwd,'String'));

HandleEditTextSV=findobj(gcf,'Tag','EditTextSV');
SVsave=str2num(get(HandleEditTextSV,'String'));

HandleEditTextPsys=findobj(gcf,'Tag','EditTextPsys');
Psyssave=str2num(get(HandleEditTextPsys,'String'));

HandleEditTextPdia=findobj(gcf,'Tag','EditTextPdia');
Pdiasave=str2num(get(HandleEditTextPdia,'String'));

HandleEditTextPmean=findobj(gcf,'Tag','EditTextPmean');
Pmeansave=str2num(get(HandleEditTextPmean,'String'));

HandleEditTextLVEDV=findobj(gcf,'Tag','EditTextLVEDV');
LVEDVsave=str2num(get(HandleEditTextLVEDV,'String'));

HandleEditTextAOLeak=findobj(gcf,'Tag','EditTextAOLeak');
AOLeaksave=str2num(get(HandleEditTextAOLeak,'String'));

HandleEditTextAOFwd=findobj(gcf,'Tag','EditTextAOFwd');
AOFwdsave=str2num(get(HandleEditTextAOFwd,'String'));

HandleEditTextMVLeak=findobj(gcf,'Tag','EditTextMVLeak');
MVLeaksave=str2num(get(HandleEditTextMVLeak,'String'));

HandleEditTextMVFwd=findobj(gcf,'Tag','EditTextMVFwd');
MVFwdsave=str2num(get(HandleEditTextMVFwd,'String'));

HandleEditTextPLVleak=findobj(gcf,'Tag','EditTextPLVleak');
PLVleaksave=str2num(get(HandleEditTextPLVleak,'String'));

HandleEditTextPaoleak=findobj(gcf,'Tag','EditTextPaoleak');
Paoleaksave=str2num(get(HandleEditTextPaoleak,'String'));

HandleEditTextTAOLeak=findobj(gcf,'Tag','EditTextTAOLeak');
TAOLeaksave=str2num(get(HandleEditTextTAOLeak,'String'));

HandleEditTextTMVLeak=findobj(gcf,'Tag','EditTextTMVLeak');
TMVLeaksave=str2num(get(HandleEditTextTMVLeak,'String'));

[FILENAME, PATHNAME] = uiputfile('*.txt', 'Write results in File');
fname=[PATHNAME FILENAME '.txt'];
[fid1,message]=fopen(fname,'w');

if fid1==-1
   error('File %s could not be opened',fname);
end;

fprintf(fid1,'HR\t%g\n',HR);								% heart period = duration 1 cycle
fprintf(fid1,'Emax ventricle\t%g\n',EmaxV);			% Emax ventricle
fprintf(fid1,'TEmax ventricle\t%g\n',TEmaxV);		% time Emax ventricle
fprintf(fid1,'Emin ventricle\t%g\n',EminV);			% Emin ventricle
fprintf(fid1,'Vdead ventricle\t%g\n',VdeadV);		% dead LV volume
fprintf(fid1,'Pvenous\t%g\n',Pvenous);					% LA filling pressure
fprintf(fid1,'Rmv forward\t%g\n',Rmvfwd);				% mitral valve resistance
fprintf(fid1,'Rao forward\t%g\n',Raofwd);				% aortic valve resistance
fprintf(fid1,'Rmv backward\t%g\n',Rmvbwd);			% backward mitral valve resistance
fprintf(fid1,'Rao backward\t%g\n',Raobwd);			% backward aortic valve resistance
fprintf(fid1,'Z0\t%g\n',Z0);								% characteristic impedance
fprintf(fid1,'L\t%g\n',L);									% inertance	% if L = 0, use a 3-element WK model 
fprintf(fid1,'C\t%g\n',C);									% compliance
fprintf(fid1,'R\t%g\n',R);									% resistance
fprintf(fid1,'SV\t%g\n',SVsave);							% stroke volume
fprintf(fid1,'Psys\t%g\n',Psyssave);					% aortic systolic pressure
fprintf(fid1,'Pdia\t%g\n',Pdiasave);					% aortic diastolic pressure
fprintf(fid1,'Pmean\t%g\n',Pmeansave);					% mean aortic pressure
fprintf(fid1,'LV EDV\t%g\n',LVEDVsave);				% LV end-diastolic volume
fprintf(fid1,'AO Leak\t%g\n',AOLeaksave);				% AO Leak volume
fprintf(fid1,'AO Fwd\t%g\n',AOFwdsave);				% AO Forward volume
fprintf(fid1,'Time AO Leak\t%g\n',TAOLeaksave);		% Time of aortic leak
fprintf(fid1,'MV Leak\t%g\n',MVLeaksave);				% MV Leak volume
fprintf(fid1,'MV Fwd\t%g\n',MVFwdsave);				% MV Forward volume
fprintf(fid1,'Time MV Leak\t%g\n',TMVLeaksave);		% Time of mitral leak
fprintf(fid1,'PLV during leak\t%g\n',PLVleaksave);	% mean PLV during leak 
fprintf(fid1,'Pao during leak\t%g\n',Paoleaksave);	% mean Pao during leak 
fprintf(fid1,'time\tVLV\tPLV\tPao\tQao\n');

for k=1:length(time)
   fprintf(fid1,'%g\t%g\t%g\t%g\t%g\n',time(k),VLV(k),PLV(k),Pao(k),Qao(k));
end;

fclose('all');
